package com.wythe.mall.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtils {

    private static final String EMPTY_STRING = "";

    // 判断字符串是否是数字
    public static boolean isNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) str);
        boolean result = matcher.matches();
        return result;
    }

    //^[A-Za-z0-9]+$

    // 判断字符串是否是数字和字母
    public static boolean isNumberAndLeter(String str) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9]+$");
        Matcher matcher = pattern.matcher((CharSequence) str);
        boolean result = matcher.matches();
        return result;
    }


    private static final String normalCharRegex = "[(A-Za-z0-9\\u4E00-\\u9FA5`~!@#$%^&*()+=|{}':;',\\.\\\\<>/?~！@#￥%……&*（）——+|{}【】\\[\\]‘；：”“’。，、？《》  )]";
    //过滤掉所有非法字符
    public static String filterIllegalString(String source){
        if(!TextUtils.isEmpty(source)) {
            Matcher matcher = Pattern.compile(normalCharRegex).matcher(source);
            StringBuilder sb = new StringBuilder();
            while(matcher.find()){
                sb.append(matcher.group(0));
            }
            return sb.toString();
        }else 
            return source;
    }

	// 判断是否颜色字符串
	public static boolean isColorString(String str) {
		if (TextUtils.isEmpty(str)) {
			return false;
		}
		Pattern pattern = Pattern.compile("^#([a-fA-F0-9]{2})?[a-fA-F0-9]{6}$");
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

    public static String makeSafe(String s) {
        return (s == null) ? EMPTY_STRING : s;
    }

    /**
     * 转半角
     * @param input
     * @return
     */
    public static String toDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static boolean isMobileNO(String mobileNums) {
        /**
         * 判断字符串是否符合手机号码格式
         * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
         * 联通号段: 130,131,132,145,155,156,166,170,171,175,176,185,186
         * 电信号段: 133,149,153,170,173,177,180,181,189
         * @param str
         * @return 待检测的字符串
         */
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(16[6])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表下一位为数字可以是几，"[0-9]"代表可以为0-9中的一个，"[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }

}
